package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.activitys.CommentActivity;
import com.hxkcloud.nurse.beans.MyCollectionBean;
import com.hxkcloud.nurse.utils.ImageOptionUtils;
import com.hxkcloud.nurse.utils.ImageUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by ray_lei on 2016/7/14.
 */
public class MyCollectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{


    private Context mContext;
    private List<MyCollectionBean> datas;
    private RecyclerView recyclerview;
    private LayoutInflater mInflater;

    private ResultSelectedListener listener;

    protected ImageLoader imageLoader;
    private int screenWidth;

    public MyCollectAdapter(Context mContext, List<MyCollectionBean> datas,
                              RecyclerView recyclerview,ResultSelectedListener listener,int width) {
        this.mContext = mContext;
        this.datas = datas;
        this.recyclerview = recyclerview;
        this.listener = listener;
        this.screenWidth = width;
        mInflater = LayoutInflater.from(mContext);
        imageLoader = ImageLoader.getInstance();
    }

    public void setData(List<MyCollectionBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public void clearData() {
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return datas.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        // TODO Auto-generated method stub


        Holder mHolder = (Holder) holder;

        LinearLayout.LayoutParams lp_head = new LinearLayout.LayoutParams(screenWidth / 6, screenWidth / 6);
        lp_head.setMargins(20, 0, 0, 0);
        mHolder.icon.setLayoutParams(lp_head);
        imageLoader.displayImage(datas.get(position).getImgae(), mHolder.icon, ImageOptionUtils.getInstance().getArticlePicSmallOptions(ImageUtil.dip2px(5, mContext)));
        mHolder.titleTV.setText(datas.get(position).getTitle());
        mHolder.titleTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, CommentActivity.class);
                mContext.startActivity(intent);
            }
        });
        mHolder.descriptionTV.setText(datas.get(position).getMessage());
        mHolder.layout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                listener.onSelected("click", datas.get(position));

            }

        });

        if(datas.size()-position ==1){
            mHolder.itemView.setVisibility(View.INVISIBLE);
        }else{
            mHolder.itemView.setVisibility(View.VISIBLE);
        }




    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        View v = mInflater.inflate(R.layout.item_mycollect_layout, null);
        RecyclerView.ViewHolder holer = new Holder(v);

        return holer;
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    /*
     * Holder
     */
    public class Holder extends RecyclerView.ViewHolder {

        private LinearLayout layout;
        private ImageView icon;
        private TextView titleTV;
        private TextView descriptionTV;

        private View itemView;


        public Holder(View view) {
            super(view);

             layout = (LinearLayout) view.findViewById(R.id.mycollection_layout_itemwhole);
            icon = (ImageView) view.findViewById(R.id.mycollection_ci_icon);
            titleTV = (TextView) view.findViewById(R.id.mycollection_tv_title);
            descriptionTV = (TextView) view.findViewById(R.id.mycollection_tv_desciption);
            itemView = (View)view.findViewById(R.id.mycollection_item_view);


        }
    }

    public interface ResultSelectedListener {
        void onSelected(String type,MyCollectionBean info);
    }


}
